#include <string>
#include <vector>
using namespace std;
/*
 * @lc app=leetcode.cn id=44 lang=cpp
 *
 * [44] 通配符匹配
 */

// @lc code=start
class Solution {
public:
    bool isMatch(string s, string p) {
        // dp[i][j]表示前i个字符和前j个字符的是否匹配
        // dp[2][1] -> [2][0] || [1][1] -> dp[1][1] ->
        vector<vector<bool>> dp(s.length() + 1, vector<bool>(p.length() + 1));
        dp[0][0] = true;
        for (int j = 1; j <= p.size(); j++) {
            if (p[j - 1] == '*') dp[0][j] = true;
            else break;
        }
        for (int i = 1; i <= s.size(); i++) {
            for (int j = 1; j <= p.size(); j++) {
                if (s[i - 1] == p[j - 1] || p[j - 1] == '?') dp[i][j] = dp[i - 1][j - 1]; 
                else if (p[j - 1] == '*') dp[i][j] = dp[i][j - 1] || dp[i - 1][j] || dp[i - 1][j - 1]; 
            }
        }
        return dp[s.length()][p.length()];
    }
};
// @lc code=end

